package cn.gdcp.demo12;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Button btnOpenService;
    private Button btnCloseService;
    private Button btnBindService;
    private Button btnUnBindService;
    private Button btnadd;
    private ServiceConnection serviceConnection;

    private MyBinder myBinder;





    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnOpenService = findViewById(R.id.open_service);
        btnCloseService = findViewById(R.id.close_service);
        btnBindService = findViewById(R.id.Binder_service);
        btnUnBindService = findViewById(R.id.UnBinder_service);
        btnadd = findViewById(R.id.btn_add);


        serviceConnection = new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
                Log.e("MainActivity","onServiceConnected");
                myBinder = (MyBinder)iBinder;

            }

            @Override
            public void onServiceDisconnected(ComponentName componentName) {
                Log.e("MainActivity","onServiceDisconnected");
            }

            @Override
            public void onBindingDied(ComponentName name) {
                Log.e("MainActivity","onBindingDied");
            }
        };

        btnOpenService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                startService(intent);
            }
        });

        btnCloseService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                stopService(intent);
            }
        });

        btnBindService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setClass(MainActivity.this,MyService.class);
                bindService(intent,serviceConnection,BIND_AUTO_CREATE);
            }
        });

        btnUnBindService.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                unbindService(serviceConnection);
            }
        });

        btnadd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int isBinder = myBinder.add(200,300);
                Toast.makeText(MainActivity.this,"Binder:"+isBinder,Toast.LENGTH_SHORT).show();
                String time = myBinder.getTime();
                Toast.makeText(MainActivity.this, time, Toast.LENGTH_SHORT).show();
            }
        });

    }
}
